﻿using System;
using System.Web.Configuration;
using System.Data;

public partial class manager_task_NewDefaultAdd : System.Web.UI.Page
{
    public string Html_Dept_Detail_bm_kh = "";
    public string Html_Dept_Detail_bm_bkh = "";

    public int taskid = 0;
    public string ttitle;
    DBHelper db = new DBHelper(WebConfigurationManager.ConnectionStrings["connString"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        taskid = Convert.ToInt32(Request.QueryString["taskid"]);
        Edit_Id.Value = taskid.ToString();

        if (!IsPostBack)
        {



            loadData_Bm_Kh();
            loadData_Bm_Bkh();
            if (taskid != 0)
            {
                loadEditData(taskid);

            }
            else
            {


            }
        }
    }
    private void loadData_Bm_Kh()
    {
        dept_id.Value = "";
        DataTable dt = db.getList("v_dict_dept_position", "*", "parentid=0", "bh");



        Html_Dept_Detail_bm_kh += "<div class=\"box-body\">";
        Html_Dept_Detail_bm_kh += "<table id=\"table_Bm_Kh\" class=\"table table-bordered table-striped\">";
        Html_Dept_Detail_bm_kh += "<thead>";
        Html_Dept_Detail_bm_kh += "<tr>";
        Html_Dept_Detail_bm_kh += "<th></th>";
        Html_Dept_Detail_bm_kh += "<th class=\"col-sm-7\">名称</th>";
        Html_Dept_Detail_bm_kh += "</tr>";
        Html_Dept_Detail_bm_kh += "</thead>";
        Html_Dept_Detail_bm_kh += "<tbody>";
        foreach (DataRow dr in dt.Rows)
        {
            Html_Dept_Detail_bm_kh += "<tr style=\"cursor:pointer\" onclick=\"SetBm_Kh('" + dr["id"].ToString() + "','" + dr["mc"].ToString() + "')\">";
            Html_Dept_Detail_bm_kh += "<td>" + dr["id"].ToString() + "</td>";
            Html_Dept_Detail_bm_kh += "<td><b>" + dr["mc"].ToString() + "</b></td>";
        }
        Html_Dept_Detail_bm_kh += "</tr>";
        Html_Dept_Detail_bm_kh += "</tbody>";
        Html_Dept_Detail_bm_kh += "</table>";
        Html_Dept_Detail_bm_kh += "</div>";
        Html_Dept_Detail_bm_kh += "</div>";

    }

    private void loadData_Bm_Bkh()
    {
        dept_id.Value = "";
        DataTable dt = db.getList("v_dict_dept_position", "*", "parentid=0", "bh");
        Html_Dept_Detail_bm_bkh += "<div class=\"box-body\">";
        Html_Dept_Detail_bm_bkh += "<table id=\"table_Bm_Bkh\"  class=\"table table-bordered table-striped\">";
        Html_Dept_Detail_bm_bkh += "<thead>";
        Html_Dept_Detail_bm_bkh += "<tr>";
        Html_Dept_Detail_bm_bkh += "<th></th>";
        Html_Dept_Detail_bm_bkh += "<th class=\"col-sm-7\">名称</th>";
        Html_Dept_Detail_bm_bkh += "</tr>";
        Html_Dept_Detail_bm_bkh += "</thead>";
        Html_Dept_Detail_bm_bkh += "<tbody>";
        foreach (DataRow dr in dt.Rows)
        {
            Html_Dept_Detail_bm_bkh += "<tr style=\"cursor:pointer\" onclick=\"SetBm_Bkh('" + dr["id"].ToString() + "','" + dr["mc"].ToString() + "')\">";
            Html_Dept_Detail_bm_bkh += "<td>" + dr["id"].ToString() + "</td>";
            Html_Dept_Detail_bm_bkh += "<td><b>" + dr["mc"].ToString() + "</b></td>";
        }
        Html_Dept_Detail_bm_bkh += "</tr>";
        Html_Dept_Detail_bm_bkh += "</tbody>";
        Html_Dept_Detail_bm_bkh += "</table>";
        Html_Dept_Detail_bm_bkh += "</div>";
        Html_Dept_Detail_bm_bkh += "</div>";
    }



    private void loadEditData(int sid)
    {
        string sql = "select * from v_A_Task where id=" + sid;
        DataTable dt = db.getList(sql);
        if (dt.Rows.Count == 1)
        {

            txt_title.Value = dt.Rows[0]["title"].ToString();


            txt_title.Value = dt.Rows[0]["title"].ToString();
            txt_detail.Value = dt.Rows[0]["detail"].ToString();

            Bm_Kh.Value = dt.Rows[0]["Bm_Kh"].ToString();
            Bm_Bkh.Value = dt.Rows[0]["Bm_Bkh"].ToString();
            Bm_Kh_mc.Value = dt.Rows[0]["Bm_Kh_mc"].ToString();
            Bm_Bkh_mc.Value = dt.Rows[0]["Bm_Bkh_mc"].ToString();

            ddl_LX_sj.SelectedValue = dt.Rows[0]["LX_sj"].ToString();
            ddl_LX_kh.SelectedValue = dt.Rows[0]["LX_kh"].ToString();
            txt_rq_yjs.Value = dt.Rows[0]["rq_yjs"].ToString();
            txt_Tdata.Value = dt.Rows[0]["Tdata"].ToString();
            txt_FZ.Value = dt.Rows[0]["FZ"].ToString();

            string isusstr = "0";
            if (dt.Rows[0]["LX_sj"].ToString() == "True")
            {
                isusstr = "1";
            }
            ddl_LX_sj.SelectedValue = isusstr;

            isusstr = "0";
            if (dt.Rows[0]["LX_kh"].ToString() == "True")
            {
                isusstr = "1";
            }
            ddl_LX_kh.SelectedValue = isusstr;

            isusstr = "0";
            if (dt.Rows[0]["LX_fd"].ToString() == "True")
            {
                isusstr = "1";
            }
            ddl_LX_fd.SelectedValue = isusstr;

            isusstr = "0";
            if (dt.Rows[0]["type"].ToString() == "True")
            {
                isusstr = "1";
            }
            ddl_Type.SelectedValue = isusstr;

        }


    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string errmsg = "";
        string type = ddl_Type.SelectedValue;
        string id = Edit_Id.Value;
        string title = txt_title.Value;
        string detail = txt_detail.Value;

        string TBm_Kh = Bm_Kh.Value;
        string TBm_Bkh = Bm_Bkh.Value;

        string LX_sj = ddl_LX_sj.SelectedValue;
        string LX_fd = ddl_LX_fd.SelectedValue;
        string LX_kh = ddl_LX_kh.SelectedValue;
        string rq_yjs = txt_rq_yjs.Value;
        string fb_userbh = Session[AppConfig.SESSION_USER_BH].ToString();
        string Tdata = txt_Tdata.Value;
        string fz = txt_FZ.Value;
        int Year = 0;
        int Month = 0;
        if (rq_yjs != "")
        {
            DateTime ss = Convert.ToDateTime(rq_yjs);
            SQ_Eval_Pub.GetYearMonth(ss, ref Year, ref Month);
        }

        string TRy_Kh = db.getList("select id from dict_dept_position where parentid=" + TBm_Kh).Rows[0][0].ToString();
        string TRy_Bkh = db.getList("select id from dict_dept_position where parentid=" + TBm_Bkh).Rows[0][0].ToString();

        string sql = "";
        if (id == "0")
        {
            sql = "insert into A_Task([type],[title],[detail],[Bm_Kh],[Ry_Kh],[Bm_Bkh],[Ry_Bkh],[rq_yjs],[LX_sj],[LX_kh],[fb_userbh],[Year],[Month],Tdata,LX_fd,FZ) values";
            sql += "(" + type + ",N'" + title + "',N'" + detail + "','" + TBm_Kh + "','" + TRy_Kh + "','" + TBm_Bkh + "','" + TRy_Bkh + "','" + rq_yjs + "','" + LX_sj + "','" + LX_kh + "','" + fb_userbh + "','" + Year + "','" + Month + "','" + Tdata + "','" + LX_fd + "'," + fz + ")";
        }
        else
        {
            sql = "update A_Task set type=" + type + ",[title]=N'" + title + "',[detail]=N'" + detail + "',[Bm_Kh]='" + TBm_Kh + "',[Ry_Kh]='" + TRy_Kh + "',[Bm_Bkh]='" + TBm_Bkh + "',Ry_Bkh=" + TRy_Bkh + ",[rq_yjs]='" + rq_yjs + "',[Year]='" + Year + "',[Month]='" + Month + "',Tdata='" + Tdata + "',LX_fd='" + LX_fd + "',FZ=" + fz + ",[LX_kh]=" + LX_kh + ",LX_sj=" + LX_sj + " where id=" + id + " and  [state]=0";
        }
        int i = db.executeNoneQuery(sql, ref errmsg);
        if (i > 0)
        {
            Response.Write(CommonHelper.alertMsgAndRedirect("成功", "NewDefault.aspx"));
        }
        else
        {
            Response.Write(CommonHelper.alertMsg("失败," + errmsg));
        }
    }
}